package com.hey.baseapplication.model;

import android.util.Log;

import com.hey.baseapplication.api.ApiService;
import com.hey.baseapplication.api.RetrofitClient;
import com.hey.baseapplication.bean.Response;
import com.hey.baseapplication.bean.User;

import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Hey on 16/12/15.
 */

public class LoginModel  implements  ILoginModel{
    @Override
    public Subscription login(String username, String password, final OnLoginListener loginListener) {
        return RetrofitClient.getInstance().login(ApiService.ACCESS_TOKEN,username,password,1).subscribeOn(Schedulers.newThread()).subscribe(new Subscriber<Response<User>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                Log.e("LoginModel",e.getMessage());
            }

            @Override
            public void onNext(Response<User> response) {
                loginListener.loginSuccess(response.getData());
            }
        });
    }

//    @Override1
//    public void login(String username, String password, final OnLoginListener loginListener) {
//
//        Call<Response<User>> call=RetrofitClient.getInstance().login(ApiService.ACCESS_TOKEN,username,password,1);
//        call.enqueue(new Callback<Response<User>>() {
//            @Override
//            public void onResponse(Call<Response<User>> call, retrofit2.Response<Response<User>> response) {
//                if (20000==response.body().getCode()){
//                    loginListener.loginSuccess(response.body().getData());
//                }else{
//                    loginListener.loginFailed();
//                }
//            }
//            @Override
//            public void onFailure(Call<Response<User>> call, Throwable t) {
//
//            }
//        });
//
//    }


}
